package com.lxyk.sgq.controller;

import com.lxyk.sgq.entity.Stock;
import com.lxyk.sgq.service.StockService;
import com.lxyk.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stock")
public class StockController {
    @Autowired
    private StockService stockService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/subStock/{id}")
    public AjaxResult subStock(@PathVariable("id") Long id){
        Stock stock = stockService.getById(id);
        if(stock == null){
            return AjaxResult.error("无法找到对应商品..");
        }
        stock.setCount(stock.getCount()-1);
        stockService.updateById(stock);
        return AjaxResult.success(port+"=>库存减1");
    }

    @GetMapping("/addStock/{count}")
    public AjaxResult addStock(@PathVariable("count") Integer count){
        Stock stock = new Stock();
        stock.setCount(count);
        boolean b = stockService.save(stock);
        return b == true ? AjaxResult.success("添加库存成功") : AjaxResult.error("添加库存失败");
    }
}
